package com.company;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author zhf
 * @date 2022/1/21
 */
//给定一个未排序的整数数组 nums ，找出数字连续的最长序列（不要求序列元素在原数组中连续）的长度。
    //时间复杂度为 O(n)
public class LongestCon {
    public static void main(String[] args) {
        //[9,1,4,7,3,-1,0,5,8,-1,6]
        int[] nums = new int[]{9,1,4,7,3,-1,0,5,8,-1,6};
        LongestCon longestCon = new LongestCon();
        int ans = longestCon.longestConsecutive(nums);
        System.out.println(ans);

    }
    public int longestConsecutive(int[] nums) {
        if (nums.length <= 1) {
            return nums.length;
        }
        Arrays.sort(nums);
        int max = 1;
        int temp = 1;
        int i = 1;
        while (i <= nums.length - 1) {
            if (nums[i] - nums[i - 1] == 1){
                temp++;
                i++;
            }else if(nums[i] - nums[i - 1] == 0){
                i++;
            }else {
                temp = 1;
                i++;
            }
            max = Math.max(temp,max);
        }
        return max;

    }
}
